package angry.courses.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.EJB;

import angry.courses.entities.StudentEntity;

@Stateless(mappedName="angry.courses.beans.StudentBean")
@Remote
public class StudentBean implements Student {
	@EJB
	private StudentDAO studentDAO;

	public boolean validateStudent(String name, String regnr) {
		StudentEntity student = studentDAO.retrieve(regnr);
		return student != null && student.getName().equals(name);
	}
	
	public StudentEntity getStudent(String regnr) {
		return studentDAO.retrieve(regnr);
	}
	
	public StudentEntity getStudentByName(String name) {
		return studentDAO.retrieveByName(name);
	}
	
	public void deleteStudent(StudentEntity student) {
		studentDAO.remove(student);
	}

	public List<String> getStudentNames() {
		List<StudentEntity> students = studentDAO.retrieveStudents();
		if (students != null) {
			List<String> studentNames = new ArrayList<String>();
			for (StudentEntity student : students) {
				studentNames.add(student.getName());
			}
			return studentNames;
		}
		return null;
	}
}
